/* Copyright 2013 (C) FatalEror / Dean Bahari
 * 
 * jroCK is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * See LICENSE file in main folder for more information.
 */
package jrock.other;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;

/**
 * @author FatalEror
 */
public class Utility {
    /*
     * Return a String object from buffer with spesific
     * offset and length. Gravity uses string with fixed
     * length that ended with null byte thus this should
     * be used to read it.
     * 
     * @param   input   buffer that store string data
     * @param   offset  offset from buffer current position
     * @param   length  fixed length of string
     * @return          string with correct charset and length
     */
    public static String read(ByteBuffer input, int offset, int length) {
        CharBuffer tmpName = CharBuffer.allocate(length);
        byte       tmpByte;
        while ((tmpByte = input.get()) != 0) {
           tmpName.put((char) (((char)tmpByte) & 0xFF));
        }
        tmpName.flip();
        input.position(input.position() + tmpName.capacity() - tmpName.length() - 1);

        return tmpName.toString();
    }
}
